package com.ruoyi.wechat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.domain.Product;
import com.ruoyi.wechat.domain.Spec;
import com.ruoyi.wechat.domain.dto.ProductDTO;
import com.ruoyi.wechat.domain.vo.AjaxResult;
import com.ruoyi.wechat.service.IProductService;
import com.ruoyi.wechat.service.ISpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.wechat.domain.vo.AjaxResult.success;

/**
 * 商品Controller
 *
 * @author YinHeng
 * @date 2024-01-13
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService goodsService;

    @Autowired
    private ISpecService specService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    public AjaxResult list(Product req) {
        Page<ProductDTO> dtoPage = goodsService.page(req);
        return success(dtoPage);
    }


    /**
     * 获取商品详细信息
     */
    @GetMapping("/detail")
    public AjaxResult getDetail(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return AjaxResult.error("缺少参数id");
        }
        return success(goodsService.detail(id));
    }

    /**
     * 新增商品
     */
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        return AjaxResult.success(goodsService.insertTaskItem(product));
    }

    /**
     * 修改商品
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        return AjaxResult.success(goodsService.updateTaskItem(product));
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(goodsService.deleteTaskItemByIds(ids));
    }

    @PostMapping("/add-items")
    public AjaxResult addItems() {
        List<Product> pList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            Product product = new Product();
            product.setCategory(Constants.Product.CATEGORY_3.getValue());
            product.setCover("https://meta-task.oss-cn-chengdu.aliyuncs.com/wechat/sbl/order/img/u=1220902177,2127634902&fm=224&app=112&f=JPEG.jpg.crdownload");
            product.setName("商品" + i);
            product.setOnOffStatus(Constants.Product.ON_OFF_STATUS_1.getValue());
            pList.add(product);
        }
        goodsService.saveBatch(pList);
        return AjaxResult.success(pList);
    }


    @PostMapping("/add-specs")
    public AjaxResult addSpecs() {
        LambdaQueryWrapper<Product> pdQuery = new LambdaQueryWrapper<>();
        pdQuery.eq(Product::getStatus,Constants.Common.STATUS_1.getValue());
        List<Product> pList = goodsService.list(pdQuery);
        List<Spec> specList = new ArrayList<>();
        pList.forEach(product -> {
            Spec spec = new Spec();
            spec.setStock(99999);
            spec.setItemId(product.getId());
            spec.setPriceNormal(BigDecimal.valueOf(0.3));
            spec.setCreatedTime(LocalDateTime.now());
            spec.setUpdatedTime(LocalDateTime.now());
            specList.add(spec);
        });
        specService.saveBatch(specList);

        return AjaxResult.success(specList);
    }
}
